package cn.jsj.invite.controller;

import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.pojo.identity.UserIdentityEnum;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/student")
public class StudentController {

    private final StudentService studentService;
    private final RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    public StudentController(StudentService studentService, RedisTemplate<String, Serializable> redisTemplate) {
        this.studentService = studentService;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 学生注册
     *
     * @param studentEnt 学生实体类
     * @return 1：成功 0：失败
     */
    @PassToken
    @PutMapping("/register")
    public Result<String> addStudent(@RequestBody StudentEnt studentEnt) {
        int isFalse = studentService.registerStudent(studentEnt);
        if (isFalse == 1) {//注册成功
            return Result.success("1");
        } else { //注册失败
            return Result.error("0");
        }
    }

    /**
     * 修改学生信息
     *
     * @param studentEnt 学生信息
     * @return 1：修改成功  0：失败
     */
    @PutMapping("/update")
    public Result<String> modifyOneStudentInfo(@RequestBody StudentEnt studentEnt) {
        Integer studentInfo = studentService.updateOneStudentInfo(studentEnt, studentEnt.getUpdateId().longValue(), studentEnt.getUserIdentity());
        if (studentInfo == null) {
            return Result.fail("参数缺失");
        }
        if (studentInfo.equals(-1)) {
            return Result.fail("邮箱验证码错误或者该邮箱已被占用");
        }
        if (studentInfo.equals(-2)) {
            return Result.fail("身份不符合");
        }
        if (studentInfo.equals(1)) {
            return Result.success("1"); //修改成功，返回1
        } else {
            return Result.error("0");//修改失败，返回0
        }
    }

    /**
     * 学生本人删除帖子
     *
     * @param id        帖子id
     * @param studentId 学生id
     * @return
     */
    @DeleteMapping("/deletePage")
    public Result<String> deleteStudentPageById(@RequestParam("id") Integer id, @RequestParam("studentId") Integer studentId) {
        if (studentService.deleteStudentPageById(id, studentId) == 1) {
            return Result.success("1"); //删除帖子成功，返回1
        } else {
            return Result.error("0");//删除帖子失败，返回0
        }
    }

    /**
     * 学生修改密码
     *
     * @param studentOldPassword 学生旧密码
     * @param studentNewPassword 新密码
     * @param studentId          学生id
     * @return
     */
    @PutMapping("/updatePassword")
    public Result<String> modifyStudentPwdByIdAndOldPwd(String studentOldPassword, String studentNewPassword, Integer studentId) {
        if (studentService.updateStudentPwdByIdAndOldPwd(studentOldPassword, studentNewPassword, studentId) == 1) {
            return Result.success("1"); //修改密码成功，返回1
        } else {
            return Result.error("0");//修改密码失败，返回0
        }
    }

    //根据学生ID查询自己所有的帖子
    @PostMapping("/findPage/myself")
    public Result<?> studentFindMyPage(Integer stuId) {
        List<InvitationInfo> invitationInfos = studentService.listInvitationInfoByStudentId(stuId);
        if (invitationInfos.size() == 0) {
            return Result.fail("查无数据");
        }
        return Result.success(invitationInfos);
    }

    //学生隐私开关，分开和关2个状态
    @PostMapping("/privacy/{studentId}/{isOpen}")
    public Result<?> studentPrivacy(@PathVariable Integer studentId, @PathVariable Integer isOpen) {
        if (studentService.studentPrivacySwitch(studentId, isOpen).equals(1)) {
            return Result.success();
        }
        return Result.fail("修改失败");
    }


}
